﻿/* 分页类 by match on 20110425 */
namespace ShopB2C.Core.Helpers {
    using System;
    using System.Web.UI;
    using ShopB2C.Core.Entities;
    using ShopB2C.Core.Enums;

    /// <summary>
    /// 分页
    /// </summary>
    internal class PagerHelper : IDisposable {

        private Int32 _pageCount;
        private PagerParams _params;
        private HtmlTextWriter _writer;

        public PagerHelper(PagerParams pagerParams) {
            this._params = pagerParams;
            this._writer = new HtmlTextWriter(pagerParams.TextWriter);
        }

        #region Methods

        // 组合 Url
        private void CreateUrl() {
            string separator = "?";
            if (this._params.Url.Contains("?")) {
                separator = "&";
            }
            this._params.Url += (separator + "p={0}&s={1}");
        }
        // 总算总页数
        private void CalculatePageCount() {
            if (this._params.RecordCount <= 0 || this._params.PageSize <= 0) {
                this._pageCount = 1;
            }
            else {
                this._pageCount = this._params.RecordCount / this._params.PageSize;
                if (this._params.RecordCount % this._params.PageSize > 0) {
                    this._pageCount += 1;
                }
            }
        }
        // 生成每页显示记录数列表
        private void CreatePageSizeList() {
            this._writer.RenderBeginTag(HtmlTextWriterTag.Span);
            foreach (string s in this._params.PageSizeList.Split(',')) {
                if (int.Parse(s) == this._params.PageSize) {
                    this.CreateSpan("current", s);
                }
                else {
                    this.CreateA(string.Format(this._params.Url, this._params.PageIndex, s), string.Empty, s);
                }
            }
            this._writer.RenderEndTag();
        }
        // 输出指定的 a
        private void CreateA(string href, string title, string content) {
            this._writer.AddAttribute(HtmlTextWriterAttribute.Href, href);
            if (!string.IsNullOrEmpty(title)) {
                this._writer.AddAttribute(HtmlTextWriterAttribute.Title, title);
            }
            this._writer.RenderBeginTag(HtmlTextWriterTag.A);
            this._writer.InnerWriter.Write(content);
            this._writer.RenderEndTag();
        }
        // 输出指定的 span
        private void CreateSpan(string className, string content) {
            this._writer.AddAttribute(HtmlTextWriterAttribute.Class, className);
            this._writer.RenderBeginTag(HtmlTextWriterTag.Span);
            this._writer.InnerWriter.Write(content);
            this._writer.RenderEndTag();
        }
        // 缺省分页
        private void EntireAndSkip() {
            this.CreateUrl();
            this.CalculatePageCount();
            this._params.PageIndex = this._params.PageIndex < 1 ? 1 : this._params.PageIndex;
            this._params.PageIndex = this._params.PageIndex > this._pageCount ? this._pageCount : this._params.PageIndex;

            int minValue, maxValue;
            int preNum = 3, pageNum = 10;

            this._writer.AddAttribute(HtmlTextWriterAttribute.Class, "shopb2c_core_pager_1");
            this._writer.RenderBeginTag(HtmlTextWriterTag.Div);
            // 输出统计   
            this._writer.AddAttribute(HtmlTextWriterAttribute.Class, "l");
            this._writer.RenderBeginTag(HtmlTextWriterTag.Div);
            // 描述字符串
            if (this._params.PagerTotal == PagerTotal.LongTotal) {
                this._writer.InnerWriter.Write(string.Format(
                    this._params.Language[PagerResourceKey.LongTotal],
                    this._params.RecordCount,
                    this._pageCount,
                    this._params.PageIndex,
                    string.Empty));
                this.CreatePageSizeList();
            }
            else {
                this._writer.InnerWriter.Write(string.Format(
                    this._params.Language[PagerResourceKey.ShortTotal],
                    this._params.RecordCount,
                    this._params.PageIndex,
                    this._pageCount));
            }
            this._writer.RenderEndTag();
            // 第一页/上一页
            if (this._params.PageIndex > 1) {
                this.CreateA(string.Format(
                    this._params.Url, this._params.PageIndex - 1, this._params.PageSize),
                    this._params.Language[PagerResourceKey.Previous], "&lsaquo;");
                if (this._params.PageIndex > preNum + 1 && pageNum < this._pageCount) {
                    this.CreateA(string.Format(
                        this._params.Url, 1, this._params.PageSize), "1", this._params.PageIndex - preNum - 1 == 1 ? "1" : "1...");
                }
            }
            else {
                this.CreateSpan("disabled", "&lsaquo;");
            }
            // 循环链接
            if (pageNum > this._pageCount) {
                minValue = 1;
                maxValue = this._pageCount;
            }
            else {
                if (this._params.PageIndex > preNum) {
                    minValue = this._params.PageIndex - preNum;
                    maxValue = pageNum + minValue - 1;
                }
                else {
                    minValue = 1;
                    maxValue = pageNum;
                }
                if (maxValue > this._pageCount) {
                    minValue = this._pageCount - pageNum + 1;
                    maxValue = this._pageCount;
                }
            }
            for (int i = minValue; i <= maxValue; i++) {
                if (i == this._params.PageIndex) {
                    this.CreateSpan("current", i.ToString());
                }
                else {
                    this.CreateA(string.Format(this._params.Url, i, this._params.PageSize), i.ToString(), i.ToString());
                }
            }
            // 下一页/最后一页
            if (this._params.PageIndex < this._pageCount) {
                if (this._params.PageIndex + pageNum - preNum <= this._pageCount && maxValue < this._pageCount) {
                    this.CreateA(
                        string.Format(this._params.Url, this._pageCount, this._params.PageSize),
                        this._pageCount.ToString(),
                        this._params.PageIndex + pageNum - preNum == this._pageCount
                            ? this._pageCount.ToString()
                            : string.Format("...{0}", this._pageCount));
                }
                this.CreateA(string.Format(
                    this._params.Url, this._params.PageIndex + 1, this._params.PageSize),
                    this._params.Language[PagerResourceKey.Next], "&rsaquo;");
            }
            else {
                this.CreateSpan("disabled", "&rsaquo;");
            }
            // 跳转
            if (this._pageCount > 1) {
                this._writer.AddAttribute(HtmlTextWriterAttribute.Class, "r");
                this._writer.RenderBeginTag(HtmlTextWriterTag.Div);
                // textbox
                this._writer.AddAttribute(HtmlTextWriterAttribute.Type, "text");
                this._writer.AddAttribute(HtmlTextWriterAttribute.Class, "i");
                this._writer.AddAttribute(HtmlTextWriterAttribute.Maxlength, "5");
                this._writer.RenderBeginTag(HtmlTextWriterTag.Input);
                this._writer.RenderEndTag();
                // button
                this._writer.AddAttribute(HtmlTextWriterAttribute.Type, "button");
                this._writer.AddAttribute(HtmlTextWriterAttribute.Value, this._params.Language[PagerResourceKey.Go]);
                this._writer.AddAttribute(HtmlTextWriterAttribute.Class, "b");
                this._writer.RenderBeginTag(HtmlTextWriterTag.Input);
                this._writer.RenderEndTag();
                // hidden
                this._writer.AddAttribute(HtmlTextWriterAttribute.Value, string.Format(this._params.Url, "-p-", this._params.PageSize));
                this._writer.AddAttribute(HtmlTextWriterAttribute.Type, "hidden");
                this._writer.RenderBeginTag(HtmlTextWriterTag.Input);
                this._writer.RenderEndTag();
                this._writer.RenderEndTag();
            }
            this._writer.RenderEndTag();
        }
        // 上下页
        private void PreviousAndNext() {
            this.CreateUrl();
            this.CalculatePageCount();
            this._params.PageIndex = this._params.PageIndex < 1 ? 1 : this._params.PageIndex;
            this._params.PageIndex = this._params.PageIndex > this._pageCount ? this._pageCount : this._params.PageIndex;

            this._writer.AddAttribute(HtmlTextWriterAttribute.Class, "shopb2c_core_pager_1");
            this._writer.RenderBeginTag(HtmlTextWriterTag.Div);
            // 输出统计   
            this._writer.AddAttribute(HtmlTextWriterAttribute.Class, "l");
            this._writer.RenderBeginTag(HtmlTextWriterTag.Div);
            // 描述字符串
            if (this._params.PagerTotal == PagerTotal.LongTotal) {
                this._writer.InnerWriter.Write(string.Format(
                    this._params.Language[PagerResourceKey.LongTotal],
                    this._params.RecordCount,
                    this._pageCount,
                    this._params.PageIndex,
                    string.Empty));
                this.CreatePageSizeList();
            }
            else {
                this._writer.InnerWriter.Write(string.Format(
                    this._params.Language[PagerResourceKey.ShortTotal],
                    this._params.RecordCount,
                    this._params.PageIndex,
                    this._pageCount));
            }
            this._writer.RenderEndTag();

            // 第一页/上一页
            if (this._params.PageIndex > 1) {
                this.CreateA(string.Format(this._params.Url, 1, this._params.PageSize),
                    this._params.Language[PagerResourceKey.First],
                    this._params.Language[PagerResourceKey.First]);
                this.CreateA(string.Format(this._params.Url,
                    this._params.PageIndex - 1, this._params.PageSize),
                    this._params.Language[PagerResourceKey.Previous],
                    this._params.Language[PagerResourceKey.Previous]);
            }
            else {
                this.CreateSpan("disabled", this._params.Language[PagerResourceKey.First]);
                this.CreateSpan("disabled", this._params.Language[PagerResourceKey.Previous]);
            }
            // 下一页/最后一页
            if (this._params.PageIndex < this._pageCount) {
                this.CreateA(string.Format(this._params.Url, this._params.PageIndex + 1, this._params.PageSize),
                    this._params.Language[PagerResourceKey.Next],
                    this._params.Language[PagerResourceKey.Next]);
                this.CreateA(string.Format(this._params.Url, this._pageCount, this._params.PageSize),
                    this._params.Language[PagerResourceKey.Last],
                    this._params.Language[PagerResourceKey.Last]);
            }
            else {
                this.CreateSpan("disabled", this._params.Language[PagerResourceKey.Next]);
                this.CreateSpan("disabled", this._params.Language[PagerResourceKey.Last]);
            }
            this._writer.RenderEndTag();
        }
        /// <summary>
        /// 创建分页
        /// </summary>
        /// <returns>string</returns>
        public void Create() {
            if (this._params.PagerType == PagerType.EntireAndSkip) {
                this.EntireAndSkip();
            }
            else if (this._params.PagerType == PagerType.PreviousAndNext) {
                this.PreviousAndNext();
            }
        }
        /// <summary>
        /// 释放资源
        /// </summary>
        public void Dispose() {
            this._writer.Close();
            this._writer.Dispose();
        }

        #endregion
    }
}
